package com.lutzyt.delivery.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author zyt
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> {
  private Integer code;
  private String message;
  private T data;

  public static Result<Void> success(String message) {
    Result<Void> result = new Result<Void>();
    result.setCode(200);
    result.setMessage(message);
    return result;
  }

  public static Result<Void> success() {
    Result<Void> result = new Result<Void>();
    result.setCode(200);
    result.setMessage("成功");
    return result;
  }

  public static Result<Void> fail(String message) {
    Result<Void> result = new Result<Void>();
    result.setCode(100);
    result.setMessage(message);
    return result;
  }

  public static <T> Result<T> fail(T data,String message) {
    Result<T> result = new Result<>();
    result.setCode(100);
    result.setMessage(message);
    result.setData(data);
    return result;
  }

  public static Result<Void> fail() {
    Result<Void> result = new Result<Void>();
    result.setCode(100);
    return result;
  }

  public static <T> Result<T> success(T data) {
    Result<T> result = new Result<T>();
    result.setCode(200);
    result.setData(data);
    return result;
  }

  public static <T> Result<T> success(T data,String message) {
    Result<T> result = new Result<T>();
    result.setCode(200);
    result.setMessage(message);
    result.setData(data);
    return result;
  }

  /**
   * 未认证-》登录
   *
   * @return
   */
  public static Result<Void> unAuthenticated() {
    return new Result<>(401, "未登录，请先登录！", null);
  }

  /**
   * 未授权-》无权限
   *
   * @return
   */
  public static Result<Void> unAuthorized() {
    return new Result<>(403, "无权限，请确认后在操作！", null);
  }
}
